package com.sunnypower.rushfitapp;

import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.JsonArray;
import com.sunnypower.rushfitapp.Coursepart.CoachListAdapter;
import com.sunnypower.rushfitapp.entity.Coach;
import com.sunnypower.rushfitapp.entity.User;
import com.sunnypower.rushfitapp.util.GetData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CoachActivity extends AppCompatActivity {

    private List<Coach> mCoachBeanList;
    private CoachListAdapter adapter;
    private ListView lv_coach;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coach);

        mCoachBeanList = new ArrayList<>();
        try {
            initCoachData();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        lv_coach = findViewById(R.id.lv_coach);
        adapter = new CoachListAdapter(CoachActivity.this, mCoachBeanList);
        lv_coach.setAdapter(adapter);

    }

    private void initCoachData() throws JSONException {
        String data = GetData.doGet("http://120.24.24.141:10990/rushfit/FindAllCoach2app");
        System.out.println(data);
        //解析json数据
        System.out.println("responseData：" + data);
        JSONArray CoachList = null;
        try {
            JSONObject jsonObject = new JSONObject(data);
            System.out.println("jsonObject：" + jsonObject);
            CoachList = jsonObject.getJSONArray("AllCoach");
            System.out.println("CoachList：" + CoachList);
        } catch (JSONException ex) {
            ex.printStackTrace();
        }

        for (int i = 0; i < CoachList.length(); i++) {
            JSONObject jsonCoach = CoachList.getJSONObject(i);
            Coach bean = new Coach();
            bean.setAward(jsonCoach.getString("award"));
            bean.setInfo(jsonCoach.getString("info"));
            bean.setName(jsonCoach.getString("name"));
            bean.setPhoto(jsonCoach.getString("photo"));
            bean.setSex(jsonCoach.getInt("sex"));
            mCoachBeanList.add(bean);
        }
    }
}
